﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;
using System.Collections.Generic;
using System.Web.Configuration;

namespace HoaDon
{
	public partial class PXAInvoice : System.Web.UI.Page
	{
		protected void Page_Init(object sender, EventArgs e)
		{
			BlindListView_NhomMon();
		}

		protected void Page_Load(object sender, EventArgs e)
		{
			if (!IsPostBack)
			{
				if (Session["PXAchange"] == null)
					Session["PXAchange"] = "0";
			}
			AddEventInButton_MonAn();
		}

		protected void Page_PreRender(object sender, EventArgs e)
		{
			if (!IsPostBack)
			{
				DataTable dtKho = new DataTable();
				string sCommandKho = "SELECT KhoID, KhoName FROM dmkho WHERE FK_DatabaseID = '" + Session["FK_DatabaseID"].ToString() + "' AND Status = '1'";
				if (Session["ListKhoID"].ToString() != "")
				{
					sCommandKho = sCommandKho + " AND (";
					for (int i = 0; i < Session["ListKhoID"].ToString().Split(',').Length; i++)
					{
						sCommandKho = sCommandKho + "KhoID = '" + Session["ListKhoID"].ToString().Split(',')[i] + "' OR ";
					}
					sCommandKho = sCommandKho + " 1=0)";
				}
				dtKho = SqlEXEC.GetData(Session["strConn"].ToString(), sCommandKho);
				drkho.DataSource = dtKho;
				drkho.DataValueField = "KhoID";
				drkho.DataTextField = "KhoName";
				drkho.DataBind();
				if (Session["PXAKho"] != null)
				{
					drkho.SelectedValue = Session["PXAKho"].ToString();
				}
				else
				{
					if (drkho.SelectedValue != null)
						Session["PXAKho"] = drkho.SelectedValue;
				}

				DataTable dtPh = new DataTable();
				dtPh = SqlEXEC.GetData(Session["strConn"].ToString(), "SELECT * FROM ph84 WHERE FK_DatabaseID = '" + Session["FK_DatabaseID"].ToString() + "' AND Status='0'");

				if (dtPh.Rows.Count > 0)
				{
					if (dtPh.Rows[0]["Dien_Giai"] != DBNull.Value)
						txtDienGiai.Text = dtPh.Rows[0]["Dien_Giai"].ToString();
					if (dtPh.Rows[0]["FK_khoID"] != DBNull.Value)
					{
						Session["PXAKho"] = dtPh.Rows[0]["FK_khoID"].ToString();
						drkho.SelectedValue = dtPh.Rows[0]["FK_khoID"].ToString();
					}
				}
				else
				{
					txtDienGiai.Text = "";
				}
			}
			BlindGridView_MonDaChon();
		}

		private void BlindListView_NhomMon()
		{
			DataTable dtNhvt2 = new DataTable();
			dtNhvt2 = SqlEXEC.GetData(Session["strConn"].ToString(), "SELECT Nhvt2ID, Nhvt2Name FROM dmnhvt2 WHERE FK_DatabaseID = '" + Session["FK_DatabaseID"].ToString() + "' AND TP_yn = 0 AND NL_yn = 1 AND Status = 1");

			if ((dtNhvt2.Rows.Count > 0))
			{
				if (Session["PXANhomMon"] == null)
					Session["PXANhomMon"] = dtNhvt2.Rows[0]["nhvt2Id"].ToString();
				for (int i = 0; i < dtNhvt2.Rows.Count; i++)
				{
					Button btnNhomMon = new Button();
					btnNhomMon.Text = dtNhvt2.Rows[i]["nhvt2Name"].ToString();
					btnNhomMon.ID = "N" + dtNhvt2.Rows[i]["nhvt2Id"].ToString();
					btnNhomMon.CssClass = "my_btn_nhommon";
					btnNhomMon.Click += btNhomMon_Click;
					tbc_nhommon.Controls.Add(btnNhomMon);
				}
			}
		}

		private void AddEventInButton_MonAn()
		{
			if (Session["PXANhomMon"] != null)
			{
				DataTable dtMon = new DataTable();
				dtMon = SqlEXEC.GetData(Session["strConn"].ToString(), "SELECT * FROM dmvt WHERE FK_DatabaseID = '" + Session["FK_DatabaseID"].ToString() + "' AND FK_Nhvt2ID = '" + Session["PXANhomMon"].ToString() + "' AND NL_yn = 1 AND TP_yn = 0 AND Status = 1");

				if ((dtMon.Rows.Count > 0))
				{
					for (int i = 0; i < dtMon.Rows.Count; i++)
					{
						Button btnMon = new Button();
						btnMon.Text = dtMon.Rows[i]["vtName"].ToString();
						btnMon.ID = "M" + dtMon.Rows[i]["vtID"].ToString();
						btnMon.CommandArgument = dtMon.Rows[i]["FK_DvtID"].ToString() + "_" + dtMon.Rows[i]["Gia_ban0"].ToString();
						btnMon.CssClass = "my_btn_mon";
						btnMon.Click += btMonan_Click;
						tbc_mon.Controls.Add(btnMon);
					}
				}
			}
		}

		private void btNhomMon_Click(Object sender, EventArgs e)
		{
			Session["PXANhomMon"] = ((Button)sender).ID.Substring(1);
			Session["PXAMon"] = null;
			Session["PXAMonName"] = null;
			Session["PXADvt_gia"] = null;
			Response.Redirect("~\\PXAInvoice.aspx");
		}

		private void btMonan_Click(Object sender, EventArgs e)
		{
			Session["PXAMon"] = ((Button)sender).ID.Substring(1);
			Session["PXAMonName"] = ((Button)sender).Text;
			Session["PXADvt_gia"] = ((Button)sender).CommandArgument;
			Session["PXAchange"] = "1";
			//Response.Redirect("~\\PXAInvoice.aspx");
		}

		private void BlindGridView_MonDaChon()
		{
			if (Session["PXAchange"] == null)
				return;
			if (Session["PXAchange"].ToString().Equals("0"))
			{
				DataTable dtPh = new DataTable();
				if (Session["NgayBD"] != null && Session["NgayKT"] != null)
				{
					dtPh = SqlEXEC.GetData(Session["strConn"].ToString(), "SELECT * FROM ph84 WHERE FK_DatabaseID = '" + Session["FK_DatabaseID"].ToString() + "' AND Status='0'");
				}
				else
				{
					string sdate = DateTime.Now.ToString("yyyyMMdd");
					dtPh = SqlEXEC.GetData(Session["strConn"].ToString(), "SELECT * FROM ph84 WHERE FK_DatabaseID = '" + Session["FK_DatabaseID"].ToString() + "' AND Status='0'");
				}
				if ((dtPh.Rows.Count > 0))
				{
					string Stt_rec = dtPh.Rows[0]["Stt_rec"].ToString();
					Session["PXAStt_rec"] = Stt_rec;
					DataTable dtCt = new DataTable();
					dtCt = SqlEXEC.GetData(Session["strConn"].ToString(), "SELECT * FROM vct84 WHERE FK_DatabaseID = '" + Session["FK_DatabaseID"].ToString() + "' AND Stt_rec = '" + Stt_rec + "'");
					gdxdsmon.DataSource = dtCt;
					gdxdsmon.DataBind();
					Session["PXAdt"] = dtCt;
				}
				else
				{
					Session["PXAdt"] = null;
				}
			}
			else
				if (Session["PXAdt"] != null)
				{
					gdxdsmon.DataSource = Session["PXAdt"] as DataTable;
					gdxdsmon.DataBind();
					if (Session["PXAMon"] != null && Session["PXAchange"].ToString().Equals("1"))
					{
						DataTable dt = Session["PXAdt"] as DataTable;
						string monan = Session["PXAMon"].ToString();
						for (int i = 0; i < dt.Rows.Count; i++)
							if (dt.Rows[i]["FK_VtID"].ToString().Equals(monan))
							{
								decimal soluong = Convert.ToDecimal(dt.Rows[i]["So_luong"].ToString()) + 1;
								decimal tien = soluong * Convert.ToDecimal(dt.Rows[i]["Gia"].ToString());

								dt.Rows[i]["So_luong"] = soluong;
								dt.Rows[i]["Tien"] = tien;
								gdxdsmon.DataSource = dt;
								gdxdsmon.DataBind();

								Session["PXAMon"] = null;
								Session["PXAdt"] = dt;
								Session["PXAchange"] = "2";
							}
						if (Session["PXAMon"] != null)
						{
							string vtu = Session["PXAMon"].ToString();
							string tenmonan = Session["PXAMonName"].ToString();
							string dvt_gia = Session["PXADvt_gia"].ToString();
							string[] s = dvt_gia.Split('_');
							string dvt = s[0];
							string gia = s[1];
							decimal sol = 1;
							decimal thanhtien = sol * Convert.ToDecimal(gia);

							DataRow NewRow = dt.NewRow();
							NewRow["Stt_rec"] = Session["PXAStt_rec"].ToString();
							NewRow["Stt_rec0"] = GetRec0(dt.Rows.Count + 1);
							NewRow["FK_DatabaseID"] = Session["FK_DatabaseID"].ToString();
							NewRow["FK_vtID"] = vtu;
							NewRow["VtName"] = tenmonan;
							NewRow["FK_dvtID"] = dvt;
							NewRow["FK_khoID"] = drkho.SelectedValue.ToString();
							NewRow["So_luong"] = sol;

							NewRow["Gia"] = gia;
							NewRow["Tien"] = thanhtien;

							NewRow["FK_TkID_vt"] = "156";

							NewRow["user_id0"] = System.Web.HttpContext.Current.User.Identity.Name.ToUpper();
							NewRow["user_id2"] = System.Web.HttpContext.Current.User.Identity.Name.ToUpper();
							NewRow["datetime0"] = DateTime.Now.Date;
							NewRow["datetime2"] = DateTime.Now.Date;

							dt.Rows.Add(NewRow);

							gdxdsmon.DataSource = dt;
							gdxdsmon.DataBind();
							Session["PXAchange"] = "2";

							Session["PXAMon"] = null;
							Session["PXAdt"] = dt;
						}
					}
				}

				else
				{
					if (Session["PXAMon"] != null)
					{
						string vtu = Session["PXAMon"].ToString();
						string tenmonan = Session["PXAMonName"].ToString();
						string dvt_gia = Session["PXADvt_gia"].ToString();
						string[] s = dvt_gia.Split('_');
						string dvt = s[0];
						string gia = s[1];
						decimal sol = 1;
						decimal nguyengia = Convert.ToDecimal(gia);
						decimal thanhtien = sol * nguyengia;

						DataTable dt = new DataTable();
						DataColumn dc = new DataColumn();

						dt = SqlEXEC.GetData(Session["strConn"].ToString(), "SELECT TOP 0 * FROM vct84");

						DataRow NewRow = dt.NewRow();
						Session["PXAStt_rec"] = GetRecID("PXA");
						NewRow["Stt_rec"] = Session["PXAStt_rec"].ToString();
						NewRow["Stt_rec0"] = GetRec0(dt.Rows.Count + 1);
						NewRow["FK_DatabaseID"] = Session["FK_DatabaseID"].ToString();
						NewRow["FK_vtID"] = vtu;
						NewRow["VtName"] = tenmonan;
						NewRow["FK_dvtID"] = dvt;
						NewRow["FK_khoID"] = drkho.SelectedValue.ToString();
						NewRow["So_luong"] = sol;

						NewRow["Gia"] = gia;
						NewRow["Tien"] = thanhtien;

						NewRow["FK_TkID_vt"] = "156";

						NewRow["user_id0"] = System.Web.HttpContext.Current.User.Identity.Name.ToUpper();
						NewRow["user_id2"] = System.Web.HttpContext.Current.User.Identity.Name.ToUpper();
						NewRow["datetime0"] = DateTime.Now.Date;
						NewRow["datetime2"] = DateTime.Now.Date;

						dt.Rows.Add(NewRow);

						gdxdsmon.DataSource = dt;
						gdxdsmon.DataBind();
						Session["PXAdt"] = dt;
						Session["PXAchange"] = "2";
						Session["PXAMon"] = null;
					}
				}

			//button giam duoc click
			if (Session["PXAchange"].ToString().Equals("3"))
			{
				//da co du lieu tren girdview 
				if (Session["PXAdt"] != null && Session["PXAStt_Rec0"] != null)
				{
					DataTable dt = Session["PXAdt"] as DataTable;
					if (dt.Rows.Count > 0)
					{
						for (int i = 0; i < dt.Rows.Count; i++)
						{
							if (dt.Rows[i]["Stt_Rec0"].ToString().Equals(Session["PXAStt_Rec0"].ToString()))
							{
								decimal soluong = Convert.ToDecimal(dt.Rows[i]["So_luong"].ToString()) - 1;
								decimal gia = Convert.ToDecimal(dt.Rows[i]["Gia"].ToString());
								decimal tien = soluong * gia;
								dt.Rows[i]["So_luong"] = soluong;
								dt.Rows[i]["Tien"] = tien;
								if (soluong == 0)
								{
									dt.Rows[i].Delete();
									dt.AcceptChanges();
								}
							}
						}
					}
					gdxdsmon.DataSource = dt;
					gdxdsmon.DataBind();
					Session["PXAMon"] = null;
					//da giam mon an
					Session["PXAdt"] = dt;
					Session["PXAchange"] = "2";
				}
			}

			decimal tongtien = 0;
			//decimal tienck = 0;
			for (int i = 0; i < gdxdsmon.Rows.Count; i++)
			{
				if (((Label)gdxdsmon.Rows[i].FindControl("Tien")).Text.Trim() != "")
					tongtien += Convert.ToDecimal(((Label)gdxdsmon.Rows[i].FindControl("Tien")).Text.ToString());
			}
			tbtongtien.Text = tongtien.ToString("#,#");
		}

		//Get Stt_rec ung voi phieu nhap kho "PXA"
		private string GetRecID(string cVoucherCode)
		{
			try
			{
				DataTable dtstt = new DataTable();
				dtstt = SqlEXEC.GetData(Session["strConn"].ToString(), "SELECT RecNumber FROM dmstt WHERE FK_DatabaseID = '" + Session["FK_DatabaseID"].ToString() + "'");
				double nOrder = Convert.ToDouble(dtstt.Rows[0]["RecNumber"]) + 1;
				string RecID = Convert.ToString(nOrder);
				int i = 9;
				int j = RecID.Trim().Length;
				while ((j < i))
				{
					RecID = "0" + RecID;
					j = j + 1;
				}
				DataTable dtopp = new DataTable();
				dtopp = SqlEXEC.GetData(Session["strConn"].ToString(), "SELECT val FROM options WHERE name = 'm_WrkStation'");
				RecID = dtopp.Rows[0]["val"].ToString() + RecID + cVoucherCode.Trim().ToUpper();
				SqlEXEC.ExecCommand(Session["strConn"].ToString(), "UPDATE dmstt SET RecNumber = " + Convert.ToString(nOrder + 1) + " WHERE FK_DatabaseID = '" + Session["FK_DatabaseID"].ToString() + "'");
				return RecID;
			}
			catch
			{
				return "";
			}
		}

		private string GetRec0(int iStt)
		{
			if (iStt < 10)
				return "00" + iStt.ToString();
			if (iStt < 100 && iStt >= 10)
				return "0" + iStt.ToString();
			return iStt.ToString();
		}

		protected void btthoanhtoan_Click(object sender, EventArgs e)
		{
			if (drkho.Items.Count <= 0)
			{
				Response.Write("<script>alert('Chưa có dữ liệu về kho, cửa hàng, không thể lưu hóa đơn');</script>");
				return;
			}
			if (Session["PXAStt_rec"] != null)
			{
				Save();
				SqlEXEC.ExecCommand(Session["strConn"].ToString(), "UPDATE ph84 SET Status= '1' WHERE FK_DatabaseID = '" + Session["FK_DatabaseID"].ToString() + "' AND Stt_rec ='" + Session["PXAStt_rec"].ToString() + "' ");
				Post(Session["PXAStt_rec"].ToString());

				Session["PXAchange"] = "2";
				Session["PXAdt"] = null;
				Session["PXAMon"] = null;
				Session["PXAMonName"] = null;
				Session["PXADvt_gia"] = null;
				Session["PXAStt_rec"] = null;
				Session["PXASTT_Rec0"] = null;
				Response.Redirect("~\\PXAInvoice.aspx");
			}
		}

		private void Post(string sSTT_Rec)
		{
			try
			{
				string text1 = "EXEC fs_PostPXA2Inventory ";
				text1 = (text1 + "'" + "PXA" + "'");
				text1 = (text1 + ", '" + "ph84" + "'");
				text1 = (text1 + ", '" + "ct84" + "'");
				text1 = (text1 + ", '" + sSTT_Rec + "'");
				text1 = (text1 + ", '" + "1" + "'");
				text1 = (text1 + ", '" + Session["FK_DatabaseID"].ToString() + "'");
				SqlEXEC.ExecCommand(Session["strConn"].ToString(), text1);
			}
			catch
			{
			}
		}

		protected void btLuu_Click(object sender, EventArgs e)
		{
			if (drkho.Items.Count <= 0)
			{
				Response.Write("<script>alert('Chưa có dữ liệu về kho, cửa hàng, không thể lưu hóa đơn');</script>");
				return;
			}
			Save();

			//Session["PXAchange"] = "2";
			//Session["PXAdt"] = null;
			//Session["PXANhomMon"] = null;
			Session["PXAMon"] = null;
			Session["PXAMonName"] = null;
			Session["PXADvt_gia"] = null;
			//Session["PXAStt_rec"] = null;
			//Session["PXASTT_Rec0"] = null;
			Response.Redirect("~\\PXAInvoice.aspx");
		}

		protected void Save()
		{
			if (Session["PXAStt_rec"] != null)
			{
				DataTable dtVv = new DataTable();
				dtVv = SqlEXEC.GetData(Session["strConn"].ToString(), "SELECT TOP 1 VvID FROM dmvv WHERE FK_DatabaseID = '" + Session["FK_DatabaseID"].ToString() + "' AND Status = '1'");
				if (dtVv.Rows.Count <= 0)
					return;
				DataTable dtCheckExist = new DataTable();
				dtCheckExist = SqlEXEC.GetData(Session["strConn"].ToString(), "SELECT * FROM ph84 WHERE FK_DatabaseID = '" + Session["FK_DatabaseID"].ToString() + "' AND Stt_rec = '" + Session["PXAStt_rec"].ToString() + "'");

				if (dtCheckExist.Rows.Count > 0)
				{
					if (Session["PXAdt"] != null)
					{
						DataTable dtCal = (DataTable)Session["PXAdt"];
						decimal tien = 0;
						decimal soluong = 0;

						for (int i = 0; i < dtCal.Rows.Count; i++)
						{
							soluong = soluong + Convert.ToDecimal(dtCal.Rows[i]["So_luong"]);
							tien = tien + (Convert.ToDecimal(dtCal.Rows[i]["Gia"]) * Convert.ToDecimal(dtCal.Rows[i]["So_luong"]));
						}

						Session["PXASoCT"] = dtCheckExist.Rows[0]["So_ct"].ToString();
						string sPh84 = "UPDATE ph84 SET ";
						sPh84 = sPh84 + "Dien_Giai = N'" + txtDienGiai.Text + "', ";
						sPh84 = sPh84 + "FK_khoID = '" + Session["PXAKho"].ToString() + "', ";
						sPh84 = sPh84 + "T_Sl = " + soluong + ", ";
						sPh84 = sPh84 + "T_Tien = " + tien + ", ";
						sPh84 = sPh84 + "user_id2 = '" + System.Web.HttpContext.Current.User.Identity.Name.ToUpper() + "', ";
						sPh84 = sPh84 + "datetime2 = '" + DateTime.Now.ToString("yyyyMMdd") + "' ";

						sPh84 = sPh84 + "WHERE FK_DatabaseID = '" + Session["FK_DatabaseID"].ToString() + "' AND Stt_rec = '" + Session["PXAStt_rec"].ToString() + "'";
						SqlEXEC.ExecCommand(Session["strConn"].ToString(), sPh84);

						DataTable dt = Session["PXAdt"] as DataTable;
						SqlEXEC.ExecCommand(Session["strConn"].ToString(), "DELETE FROM ct84 WHERE FK_DatabaseID = '" + Session["FK_DatabaseID"].ToString() + "' AND Stt_Rec = '" + Session["PXAStt_rec"].ToString() + "'");
						for (int i = 0; i < dt.Rows.Count; i++)
						{
							string sCommand = "INSERT INTO ct84 (Stt_rec, Stt_rec0, FK_DatabaseID, FK_vtID, FK_dvtID, FK_KhoID, Dien_giai, Ton_kho, So_luong, Gia, Tien, FK_TkID_vt, user_id0, user_id2, datetime0, datetime2) VALUES ('";
							sCommand = sCommand + Session["PXAStt_rec"].ToString() + "','";
							sCommand = sCommand + dt.Rows[i]["Stt_rec0"].ToString() + "','";
							sCommand = sCommand + Session["FK_DatabaseID"].ToString() + "','";
							sCommand = sCommand + dt.Rows[i]["FK_vtID"].ToString() + "',N'";
							sCommand = sCommand + dt.Rows[i]["FK_dvtID"].ToString() + "','";
							sCommand = sCommand + dt.Rows[i]["FK_KhoID"].ToString() + "',N'";
							sCommand = sCommand + "" + "',";
							sCommand = sCommand + "0" + ",";
							sCommand = sCommand + dt.Rows[i]["So_luong"].ToString() + ",";
							sCommand = sCommand + dt.Rows[i]["Gia"].ToString() + ",";
							sCommand = sCommand + dt.Rows[i]["Tien"].ToString() + ",'";
							sCommand = sCommand + dt.Rows[i]["FK_TkID_vt"].ToString() + "','";
							sCommand = sCommand + dt.Rows[i]["user_id0"].ToString() + "','";
							sCommand = sCommand + dt.Rows[i]["user_id2"].ToString() + "','";
							sCommand = sCommand + Convert.ToDateTime(dt.Rows[i]["datetime0"]).ToString("yyyyMMdd") + "','";
							sCommand = sCommand + Convert.ToDateTime(dt.Rows[i]["datetime2"]).ToString("yyyyMMdd") + "')";
							SqlEXEC.ExecCommand(Session["strConn"].ToString(), sCommand);
						}
					}
				}
				else
				{
					if (Session["PXAdt"] != null)
					{
						DataTable dt = Session["PXAdt"] as DataTable;
						if (dt.Rows.Count > 0)
						{
							DataTable Ds2 = new DataTable();
							string text2 = "EXEC fs_HG_GET_SCT 'PXA','" + Session["FK_DatabaseID"].ToString() + "'";
							Ds2 = SqlEXEC.GetData(Session["strConn"].ToString(), text2);
							SqlEXEC.ExecCommand(Session["strConn"].ToString(), "UPDATE dmct SET No = (No + 1) WHERE FK_DatabaseID = '" + Session["FK_DatabaseID"].ToString() + "' AND CtID = 'PXA'");
							Session["PXASoCT"] = Ds2.Rows[0]["So_ct"].ToString();

							DataTable dtCal = (DataTable)Session["PXAdt"];
							decimal tien = 0;
							decimal soluong = 0;
							for (int i = 0; i < dtCal.Rows.Count; i++)
							{
								soluong = soluong + Convert.ToDecimal(dtCal.Rows[i]["So_luong"]);
								tien = tien + (Convert.ToDecimal(dtCal.Rows[i]["Gia"]) * Convert.ToDecimal(dtCal.Rows[i]["So_luong"]));
							}

							string sPh84 = "INSERT INTO ph84 (Stt_rec, FK_DatabaseID, Stt_rec_dc, FK_CtID, Ngay_ct, So_ct, Code, Ong_Ba, Dia_Chi, Dien_Giai, FK_NxID, FK_TkID, FK_doitacID, FK_khoID, FK_VvID, FK_bpkdID, T_Sl, T_Tien, Status, user_id0, user_id2, datetime0, datetime2) VALUES('";
							sPh84 = sPh84 + Session["PXAStt_rec"].ToString() + "','";
							sPh84 = sPh84 + Session["FK_DatabaseID"].ToString() + "','','";
							sPh84 = sPh84 + "PXA" + "','";
							sPh84 = sPh84 + DateTime.Now.ToString("yyyyMMdd") + "','";
							sPh84 = sPh84 + Session["PXASoCT"].ToString() + "',N'',N'',N'',N'" + txtDienGiai.Text + "','XKK','811','AKL','";
							sPh84 = sPh84 + Session["PXAKho"].ToString() + "','";
							sPh84 = sPh84 + dtVv.Rows[0]["VvID"].ToString() + "','";
							sPh84 = sPh84 + System.Web.HttpContext.Current.User.Identity.Name.ToUpper() + "',";

							sPh84 = sPh84 + soluong + ",";
							sPh84 = sPh84 + tien + ",'";

							sPh84 = sPh84 + "0" + "','";
							sPh84 = sPh84 + System.Web.HttpContext.Current.User.Identity.Name.ToUpper() + "','";
							sPh84 = sPh84 + System.Web.HttpContext.Current.User.Identity.Name.ToUpper() + "','";
							sPh84 = sPh84 + DateTime.Now.ToString("yyyyMMdd") + "','";
							sPh84 = sPh84 + DateTime.Now.ToString("yyyyMMdd") + "')";
							SqlEXEC.ExecCommand(Session["strConn"].ToString(), sPh84);

							for (int i = 0; i < dt.Rows.Count; i++)
							{
								string sCommand = "INSERT INTO ct84 (Stt_rec, Stt_rec0, FK_DatabaseID, FK_vtID, FK_dvtID, FK_KhoID, Dien_giai, Ton_kho, So_luong, Gia, Tien, FK_TkID_vt, user_id0, user_id2, datetime0, datetime2) VALUES ('";
								sCommand = sCommand + Session["PXAStt_rec"].ToString() + "','";
								sCommand = sCommand + dt.Rows[i]["Stt_rec0"].ToString() + "','";
								sCommand = sCommand + Session["FK_DatabaseID"].ToString() + "','";
								sCommand = sCommand + dt.Rows[i]["FK_vtID"].ToString() + "',N'";
								sCommand = sCommand + dt.Rows[i]["FK_dvtID"].ToString() + "','";
								sCommand = sCommand + dt.Rows[i]["FK_KhoID"].ToString() + "',N'";
								sCommand = sCommand + "" + "',";
								sCommand = sCommand + "0" + ",";
								sCommand = sCommand + dt.Rows[i]["So_luong"].ToString() + ",";
								sCommand = sCommand + dt.Rows[i]["Gia"].ToString() + ",";
								sCommand = sCommand + dt.Rows[i]["Tien"].ToString() + ",'";
								sCommand = sCommand + dt.Rows[i]["FK_TkID_vt"].ToString() + "','";
								sCommand = sCommand + dt.Rows[i]["user_id0"].ToString() + "','";
								sCommand = sCommand + dt.Rows[i]["user_id2"].ToString() + "','";
								sCommand = sCommand + Convert.ToDateTime(dt.Rows[i]["datetime0"]).ToString("yyyyMMdd") + "','";
								sCommand = sCommand + Convert.ToDateTime(dt.Rows[i]["datetime2"]).ToString("yyyyMMdd") + "')";
								SqlEXEC.ExecCommand(Session["strConn"].ToString(), sCommand);
							}
						}
					}
				}

			}
		}

		protected void btBoqua_Click(object sender, EventArgs e)
		{
			Session["PXAchange"] = "0";
			//Session["PXAdt"] = null;
			//Session["PXANhomMon"] = null;
			Session["PXAMon"] = null;
			Session["PXAMonName"] = null;
			Session["PXADvt_gia"] = null;
			//Session["PXAStt_rec"] = null;
			Session["PXASTT_Rec0"] = null;
			Response.Redirect("~\\PXAInvoice.aspx");
		}

		protected void btTimLai_Click(object sender, EventArgs e)
		{
			Response.Redirect("~\\LocHoaDonXK.aspx");
		}

		protected void btXoa_Click(object sender, EventArgs e)
		{
			if (drkho.Items.Count <= 0)
				return;

			if (Session["PXAStt_rec"] != null)
			{
				SqlEXEC.ExecCommand(Session["strConn"].ToString(), "EXEC [fs_DeletePXA] '" + Session["PXAStt_rec"].ToString() + "', '" + Session["FK_DatabaseID"].ToString() + "', '1'");

				Session["PXAchange"] = "2";
				Session["PXAdt"] = null;
				//Session["PXANhomMon"] = null;
				Session["PXAMon"] = null;
				Session["PXAMonName"] = null;
				Session["PXADvt_gia"] = null;
				Session["PXAStt_rec"] = null;
				Session["PXASTT_Rec0"] = null;
				Response.Redirect("~\\PXAInvoice.aspx");
			}
		}

		protected void btGiam_Click(object sender, EventArgs e)
		{
			Button btn = (Button)sender;
			Session["PXASTT_Rec0"] = btn.CommandArgument;
			Session["PXAchange"] = "3";
			Response.Redirect("~\\PXAInvoice.aspx");
		}

		protected void btIn_Click(object sender, EventArgs e)
		{
		}


		protected void drKho_SelectedChanged(object sender, EventArgs objec)
		{
			if (drkho.SelectedValue != null)
			{
				Session["PXAKho"] = drkho.SelectedValue.ToString();
				if (Session["PXAdt"] != null)
				{
					DataTable dt = Session["PXAdt"] as DataTable;
					for (int i = 0; i < dt.Rows.Count; i++)
					{
						dt.Rows[i]["FK_KhoID"] = Session["PXAKho"].ToString();
					}
					dt.AcceptChanges();
					Session["PXAdt"] = dt;
				}
			}
		}

		protected void EditInvoice(object sender, GridViewEditEventArgs e)
		{
			gdxdsmon.EditIndex = e.NewEditIndex;
			BlindGridView_EditCk();
		}

		protected void CancelEditInvoice(object sender, GridViewCancelEditEventArgs e)
		{
			gdxdsmon.EditIndex = -1;
			BlindGridView_EditCk();
		}

		protected void UpdateInvoice(object sender, GridViewUpdateEventArgs e)
		{
			DataTable dt = Session["PXAdt"] as DataTable;
			int iRow = e.RowIndex;
			decimal soluong = Convert.ToDecimal(((TextBox)gdxdsmon.Rows[iRow].FindControl("tbsoluong")).Text);
			decimal gia = Convert.ToDecimal(((TextBox)gdxdsmon.Rows[iRow].FindControl("tbgia")).Text);

			dt.Rows[iRow]["So_luong"] = soluong;
			dt.Rows[iRow]["Gia"] = gia;
			dt.Rows[iRow]["Tien"] = soluong * gia;
			dt.AcceptChanges();

			Session["PXAdt"] = dt;
			gdxdsmon.EditIndex = -1;
			Session["PXAchange"] = "1";
			Response.Redirect("~\\PXAInvoice.aspx");
		}

		protected void BlindGridView_EditCk()
		{
			gdxdsmon.DataSource = Session["PXAdt"] as DataTable;
			gdxdsmon.DataBind();
		}
	}
}
